package ca.ets.osgi.hotelservice;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Calendar;

import ca.ets.osgi.hotelinterface.IHotelReservation;

public class HotelService implements IHotelReservation<Object> {

	private Calendar calen = Calendar.getInstance();
	
	public HotelService(){}
	
	
	@Override
	public Object roomInscription(String hotelName, String hotelAddress,
			String hotelArrivalDate, String hotelDepartureDate) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object roomInscription2(String hotelName, String cityEvent,
			String codePostalEvent, String hotelArrivalDate,
			String hotelDepartureDate) {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public Object roomInscription3(String cityEvent, String hotelArrivalDate,String hotelDepartureDate) {
		System.err.println("[Attempting to make an hotel reservation]");
		System.err.println("[City of event: "+ cityEvent + ", Hotel Arrival Date: " + hotelArrivalDate + ",Hotel Departure Date: " +hotelDepartureDate + "]");
		Object obj = null;
		HotelReservation reserv = null;
		
		HotelReader hReader = new HotelReader();
		hReader.openList();
		ListHotels lh = ListHotels.getInstance();
		ArrayList<HotelAbs> list = lh.getHotelsByCityAndAvailability(cityEvent);
		HotelInfo hotel = getHotel(list);
		if(hotel != null){
			reserv = hotel.getHotelReservation(hotelArrivalDate,hotelDepartureDate);
		}
		if(reserv == null)
			reserv= null;
		obj = convertHotelToObject(reserv);
		return obj;
	}
	
	private HotelInfo getHotel(ArrayList<HotelAbs> list){
		HotelInfo hotel = null;
		if(list.size()!=0){
			BufferedReader inBR2 = new BufferedReader(new InputStreamReader(System.in));
			String str;
			while(true){
				try {
					System.out.println("Your Choice are: ");
					for(int i = 0; i < list.size();i++){
						System.out.println("["+i+"]" + list.get(i).toString());
					}
					System.out.println("Which suits you best");
					str = inBR2.readLine();
					str.trim();
					int choice = Integer.parseInt(str);
					if( (choice >=0) && choice <list.size()){
						hotel = (HotelInfo) list.get(choice);
						System.out.println("Reservation made to the hotel: " + hotel.getHotelName());
						System.out.println("On: " + calen.getTime());
						break;
					}
					System.err.println("WRONG CHOICE!!!!");
					System.err.println("");
					
					
					
					//inBR2.close();
					
					
				}catch(IndexOutOfBoundsException i){
					//System.err.println("WRONG CHOICE!!!!");
				}catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		else if(list.size()==0)
			System.out.println("Sorry, no hotel were found");
		
		return hotel;
	}
	
	/**
	 * Return a convert hotel objet to a list of object, containing hotel information
	 * @param hotel
	 * @return
	 */
	private ArrayList<Object> convertHotelToObject(HotelReservation hotel){
		ArrayList<Object> list = new ArrayList<Object>();
		if(hotel !=null){			
			list.add(hotel.getHotelID());
			list.add(hotel.getHotelName());
			list.add(hotel.getPhone());
			list.add(hotel.getDateStart());
			list.add(hotel.getDateEnd());
			list.add(hotel.getAdresse().getAdresseNum());
			list.add(hotel.getAdresse().getRue());
			list.add(hotel.getAdresse().getVille());
			list.add(hotel.getAdresse().getProvince());
			list.add(hotel.getAdresse().getCodePostal());
		
		}
			
		return list;
	}


	

}
